<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>webchannelbasetransport_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_net_webchannel_webchannelbasetransport_test.js.html">webchannelbasetransport_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Unit tests for goog.labs.net.webChannel.WebChannelBase.
<a name="line17"></a> * @suppress {accessControls} Private methods are accessed for test purposes.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.labs.net.webChannel.webChannelBaseTransportTest&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.events&#39;);
<a name="line25"></a>goog.require(&#39;goog.labs.net.webChannel.WebChannelBaseTransport&#39;);
<a name="line26"></a>goog.require(&#39;goog.net.WebChannel&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line28"></a>
<a name="line29"></a>goog.setTestOnly(&#39;goog.labs.net.webChannel.webChannelBaseTransportTest&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>var webChannel;
<a name="line33"></a>var channelUrl = &#39;http://127.0.0.1:8080/channel&#39;;
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>function setUp() {
<a name="line37"></a>}
<a name="line38"></a>
<a name="line39"></a>function tearDown() {
<a name="line40"></a>  goog.dispose(webChannel);
<a name="line41"></a>}
<a name="line42"></a>
<a name="line43"></a>function testOpenWithUrl() {
<a name="line44"></a>  var webChannelTransport =
<a name="line45"></a>      new goog.labs.net.webChannel.WebChannelBaseTransport();
<a name="line46"></a>  webChannel = webChannelTransport.createWebChannel(channelUrl);
<a name="line47"></a>
<a name="line48"></a>  var eventFired = false;
<a name="line49"></a>  goog.events.listen(webChannel, goog.net.WebChannel.EventType.OPEN,
<a name="line50"></a>      function(e) {
<a name="line51"></a>        eventFired = true;
<a name="line52"></a>      });
<a name="line53"></a>
<a name="line54"></a>  webChannel.open();
<a name="line55"></a>  assertFalse(eventFired);
<a name="line56"></a>
<a name="line57"></a>  var channel = webChannel.channel_;
<a name="line58"></a>  assertNotNull(channel);
<a name="line59"></a>
<a name="line60"></a>  simulateOpenEvent(channel);
<a name="line61"></a>  assertTrue(eventFired);
<a name="line62"></a>}
<a name="line63"></a>
<a name="line64"></a>function testOpenWithTestUrl() {
<a name="line65"></a>  var webChannelTransport =
<a name="line66"></a>      new goog.labs.net.webChannel.WebChannelBaseTransport();
<a name="line67"></a>  var options = {&#39;testUrl&#39;: channelUrl + &#39;/footest&#39;};
<a name="line68"></a>  webChannel = webChannelTransport.createWebChannel(channelUrl, options);
<a name="line69"></a>  webChannel.open();
<a name="line70"></a>
<a name="line71"></a>  var testPath = webChannel.channel_.connectionTest_.path_;
<a name="line72"></a>  assertNotNullNorUndefined(testPath);
<a name="line73"></a>}
<a name="line74"></a>
<a name="line75"></a>function testOpenWithCustomHeaders() {
<a name="line76"></a>  var webChannelTransport =
<a name="line77"></a>      new goog.labs.net.webChannel.WebChannelBaseTransport();
<a name="line78"></a>  var options = {&#39;messageHeaders&#39;: {&#39;foo-key&#39;: &#39;foo-value&#39;}};
<a name="line79"></a>  webChannel = webChannelTransport.createWebChannel(channelUrl, options);
<a name="line80"></a>  webChannel.open();
<a name="line81"></a>
<a name="line82"></a>  var extraHeaders_ = webChannel.channel_.extraHeaders_;
<a name="line83"></a>  assertNotNullNorUndefined(extraHeaders_);
<a name="line84"></a>}
<a name="line85"></a>
<a name="line86"></a>function testOpenWithCustomParams() {
<a name="line87"></a>  var webChannelTransport =
<a name="line88"></a>      new goog.labs.net.webChannel.WebChannelBaseTransport();
<a name="line89"></a>  var options = {&#39;messageUrlParams&#39;: {&#39;foo-key&#39;: &#39;foo-value&#39;}};
<a name="line90"></a>  webChannel = webChannelTransport.createWebChannel(channelUrl, options);
<a name="line91"></a>  webChannel.open();
<a name="line92"></a>
<a name="line93"></a>  var extraParams = webChannel.channel_.extraParams_;
<a name="line94"></a>  assertNotNullNorUndefined(extraParams);
<a name="line95"></a>}
<a name="line96"></a>
<a name="line97"></a>function testOpenWithCorsEnabled() {
<a name="line98"></a>  var webChannelTransport =
<a name="line99"></a>      new goog.labs.net.webChannel.WebChannelBaseTransport();
<a name="line100"></a>  var options = {&#39;supportsCrossDomainXhr&#39;: true};
<a name="line101"></a>  webChannel = webChannelTransport.createWebChannel(channelUrl, options);
<a name="line102"></a>  webChannel.open();
<a name="line103"></a>
<a name="line104"></a>  assertTrue(webChannel.channel_.supportsCrossDomainXhrs_);
<a name="line105"></a>}
<a name="line106"></a>
<a name="line107"></a>function testOpenThenCloseChannel() {
<a name="line108"></a>  var webChannelTransport =
<a name="line109"></a>      new goog.labs.net.webChannel.WebChannelBaseTransport();
<a name="line110"></a>  webChannel = webChannelTransport.createWebChannel(channelUrl);
<a name="line111"></a>
<a name="line112"></a>  var eventFired = false;
<a name="line113"></a>  goog.events.listen(webChannel, goog.net.WebChannel.EventType.CLOSE,
<a name="line114"></a>      function(e) {
<a name="line115"></a>        eventFired = true;
<a name="line116"></a>      });
<a name="line117"></a>
<a name="line118"></a>  webChannel.open();
<a name="line119"></a>  assertFalse(eventFired);
<a name="line120"></a>
<a name="line121"></a>  var channel = webChannel.channel_;
<a name="line122"></a>  assertNotNull(channel);
<a name="line123"></a>
<a name="line124"></a>  simulateCloseEvent(channel);
<a name="line125"></a>  assertTrue(eventFired);
<a name="line126"></a>}
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>function testChannelError() {
<a name="line130"></a>  var webChannelTransport =
<a name="line131"></a>      new goog.labs.net.webChannel.WebChannelBaseTransport();
<a name="line132"></a>  webChannel = webChannelTransport.createWebChannel(channelUrl);
<a name="line133"></a>
<a name="line134"></a>  var eventFired = false;
<a name="line135"></a>  goog.events.listen(webChannel, goog.net.WebChannel.EventType.ERROR,
<a name="line136"></a>      function(e) {
<a name="line137"></a>        eventFired = true;
<a name="line138"></a>        assertEquals(goog.net.WebChannel.ErrorStatus.NETWORK_ERROR, e.status);
<a name="line139"></a>      });
<a name="line140"></a>
<a name="line141"></a>  webChannel.open();
<a name="line142"></a>  assertFalse(eventFired);
<a name="line143"></a>
<a name="line144"></a>  var channel = webChannel.channel_;
<a name="line145"></a>  assertNotNull(channel);
<a name="line146"></a>
<a name="line147"></a>  simulateErrorEvent(channel);
<a name="line148"></a>  assertTrue(eventFired);
<a name="line149"></a>}
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>function testChannelMessage() {
<a name="line153"></a>  var webChannelTransport =
<a name="line154"></a>      new goog.labs.net.webChannel.WebChannelBaseTransport();
<a name="line155"></a>  webChannel = webChannelTransport.createWebChannel(channelUrl);
<a name="line156"></a>
<a name="line157"></a>  var eventFired = false;
<a name="line158"></a>  var data = &#39;foo&#39;;
<a name="line159"></a>  goog.events.listen(webChannel, goog.net.WebChannel.EventType.MESSAGE,
<a name="line160"></a>      function(e) {
<a name="line161"></a>        eventFired = true;
<a name="line162"></a>        assertEquals(e.data, data);
<a name="line163"></a>      });
<a name="line164"></a>
<a name="line165"></a>  webChannel.open();
<a name="line166"></a>  assertFalse(eventFired);
<a name="line167"></a>
<a name="line168"></a>  var channel = webChannel.channel_;
<a name="line169"></a>  assertNotNull(channel);
<a name="line170"></a>
<a name="line171"></a>  simulateMessageEvent(channel, data);
<a name="line172"></a>  assertTrue(eventFired);
<a name="line173"></a>}
<a name="line174"></a>
<a name="line175"></a>
<a name="line176"></a>/**
<a name="line177"></a> * Simulates the WebChannelBase firing the open event for the given channel.
<a name="line178"></a> * @param {!goog.labs.net.webChannel.WebChannelBase} channel The WebChannelBase.
<a name="line179"></a> */
<a name="line180"></a>function simulateOpenEvent(channel) {
<a name="line181"></a>  assertNotNull(channel.getHandler());
<a name="line182"></a>  channel.getHandler().channelOpened();
<a name="line183"></a>}
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Simulates the WebChannelBase firing the close event for the given channel.
<a name="line188"></a> * @param {!goog.labs.net.webChannel.WebChannelBase} channel The WebChannelBase.
<a name="line189"></a> */
<a name="line190"></a>function simulateCloseEvent(channel) {
<a name="line191"></a>  assertNotNull(channel.getHandler());
<a name="line192"></a>  channel.getHandler().channelClosed();
<a name="line193"></a>}
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Simulates the WebChannelBase firing the error event for the given channel.
<a name="line198"></a> * @param {!goog.labs.net.webChannel.WebChannelBase} channel The WebChannelBase.
<a name="line199"></a> */
<a name="line200"></a>function simulateErrorEvent(channel) {
<a name="line201"></a>  assertNotNull(channel.getHandler());
<a name="line202"></a>  channel.getHandler().channelError();
<a name="line203"></a>}
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Simulates the WebChannelBase firing the message event for the given channel.
<a name="line208"></a> * @param {!goog.labs.net.webChannel.WebChannelBase} channel The WebChannelBase.
<a name="line209"></a> * @param {String} data The message data.
<a name="line210"></a> */
<a name="line211"></a>function simulateMessageEvent(channel, data) {
<a name="line212"></a>  assertNotNull(channel.getHandler());
<a name="line213"></a>  channel.getHandler().channelHandleArray(channel, data);
<a name="line214"></a>}
</pre>


</body>
</html>
